package com.endbd.purchase.purchase_order.control;

import com.endbd.feignapi.model.purchase.*;
import com.endbd.feignapi.util.PageModel;
import com.endbd.feignapi.util.ResultMessage;
import com.endbd.purchase.purchase_order.model.OtherPurchaseCondition;
import com.endbd.purchase.purchase_order.model.PurchaseDetail;
import com.endbd.purchase.purchase_order.model.PurchaseOrderCondition;
import com.endbd.purchase.purchase_order.service.PDMLService;
import com.endbd.purchase.purchase_order.service.PQueryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;


/**
 * @Auther:Lang
 * @Date:2022-11-18
 * @Time:10:31
 * @Description:控制层
 */
@RestController
@RequestMapping("/purchase")
public class PurchaseOrderAction {
    @Autowired
    private PQueryService queryService;  //采购单的查询接口自动装配

    @Autowired
    private PDMLService PDMLService;    //采购单的增、删、改的接口自动装配

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 通过登录人的id来查询对应的数据
     * 当登录人即操作人的职位为采购部经理和总经理时可以查看全部采购单，
     * 为采购部组员时，只能查看属于自己的采购单
     *
     * @param poc       查询条件
     * @param pageModel
     * @return
     */
    @RequestMapping("/view")
    public ResultMessage queryPageByCondition(PurchaseOrderCondition poc, PageModel<CPurchase> pageModel) {
        System.out.println(poc);
        PageModel<CPurchase> cPurchasePageModel = queryService.queryPurchasePageByCondition(poc, pageModel);

        ResultMessage message = new ResultMessage();
        message.setStatus("200");
        message.setFlag(true);
        message.setResultData(cPurchasePageModel);
        return message;
    }

    /**
     * 采购单审核里的审核页面  采购单管理的查看详情页面所需的详细数据
     *
     * @param pid
     * @return
     */
    @RequestMapping("/detail")
    public ResultMessage queryDetailById(@RequestParam(value = "purchaseId", required = false) String pid) {

        PurchaseDetail detail = queryService.queryPurchaseDetailById(pid);

        ResultMessage message = new ResultMessage();
        message.setFlag(true);
        message.setStatus("200");
        message.setResultData(detail);
        return message;
    }

    /**
     * 提交审核页面查询部门、职位、职员
     *
     * @return
     */
    @RequestMapping("/toExamine")
    public ResultMessage queryPositionDetails() {
        Map map = queryService.queryJobAndStaff();
        ResultMessage message = new ResultMessage();
        message.setStatus("200");
        message.setFlag(true);
        message.setResultData(map);
        return message;
    }

    /**
     * 提交审核---给谁审核
     *
     * @param uid        审核人id
     * @param purchaseId 采购单id
     * @return
     */
    @RequestMapping("/auditUser")
    public ResultMessage updatePurchaseAudit(Integer uid, String purchaseId) {
        int i = PDMLService.addAuditNameByPurchaseId(purchaseId, uid);
        ResultMessage message = new ResultMessage();
        if (i == 1) {
            message.setStatus("200");
            message.setFlag(true);
            message.setMsg(purchaseId+"提交审核成功！");
            return message;
        }
        message.setStatus("202");
        message.setFlag(false);
        message.setMsg(purchaseId+"提交审核失败！");
        return message;
    }

    /**
     * 审核采购单
     *
     * @param conn
     * @return
     */
    @RequestMapping("/audit")
    public ResultMessage auditPurchaseById(PurchaseOrderCondition conn) {
        System.out.println(conn);
        int i = PDMLService.auditById(conn);
        ResultMessage message = new ResultMessage();

        if (i == 1) {
            message.setFlag(true);
            message.setMsg(conn.getPurchaseId()+"审核成功");
            message.setStatus("200");
            return message;
        }
        message.setFlag(false);
        message.setMsg(conn.getPurchaseId()+"审核失败");
        message.setStatus("202");
        return message;
    }

    /**
     * 删除采购单
     *
     * @param purchaseId 采购单编号
     * @return
     */
    @RequestMapping("/delPurchase")
    public ResultMessage delPurchaseOrder(String purchaseId) {
        System.out.println(purchaseId);

        int i = PDMLService.delByPurchaseId(purchaseId);

        ResultMessage message = new ResultMessage();
        if (i == 1) {
            message.setStatus("200");
            message.setFlag(true);
            message.setMsg(purchaseId+"删除采购单成功！！");
            return message;
        }
        message.setStatus("202");
        message.setFlag(false);
        message.setMsg(purchaseId+"删除采购单失败！！");
        return message;
    }

    /**
     * 修改采购单
     *
     * @param purchaseId
     * @param details
     * @return
     */
    @RequestMapping("/updateDetail")
    public ResultMessage updateDetailByPurchaseId(String purchaseId, @RequestBody List<CDetails> details) {
        CPurchase purchase = new CPurchase();
        purchase.setPurchaseId(purchaseId);
        for (CDetails detail : details) {
            detail.setPurchaseId(purchase.getPurchaseId());
        }
        ResultMessage message = new ResultMessage();
        int i = PDMLService.delDetailByPurchaseIdAndProductId(purchaseId);
        int i1 = PDMLService.addPurchaseDetail(details);
        if (i == 0 || i1 == 0) {
            message.setStatus("202");
            message.setFlag(false);
            message.setMsg("修改采购单"+purchaseId+"的商品信息失败！！");
            return message;
        }
        message.setStatus("200");
        message.setFlag(true);
        message.setMsg("修改采购单"+purchaseId+"的商品信息成功！！");

        return message;
    }

    /**
     * 添加采购单
     *
     * @param purchase 采购单信息
     * @param details  采购明细
     * @return
     */
    @RequestMapping("/addPurchase")
    public ResultMessage addPurchaseOrder(CPurchase purchase, @RequestBody List<CDetails> details) {
        ResultMessage message = new ResultMessage();

//        Map<String, User> loginUser = (Map<String, User>) redisTemplate.opsForValue().get("loginUser");
//        loginUser.get(uId)

        int i = PDMLService.addPurchaseOrder(purchase);
        for (CDetails detail : details) {
            detail.setPurchaseId(purchase.getPurchaseId());
        }
        int i1 = PDMLService.addPurchaseDetail(details);
        if (i == 0 || i1 == 0) {
            message.setStatus("202");
            message.setFlag(false);
            message.setMsg("添加采购单"+purchase.getPurchaseId()+"失败！！");
            return message;
        }
        message.setStatus("200");
        message.setFlag(true);
        message.setMsg("添加采购单"+purchase.getPurchaseId()+"成功！！");

        return message;
    }

    /**
     * 根据采购单id修改仓库id、入库状态
     *
     * @param poc
     * @return
     */
    @RequestMapping("/updateRkStatus")
    public ResultMessage updateWarehouseIdAndRkStatus(PurchaseOrderCondition poc) {
        int i = PDMLService.updateWarehouseIdAndRkStatus(poc);
        ResultMessage message = new ResultMessage();
        if (i == 0) {
            message.setStatus("202");
            message.setFlag(false);
            message.setMsg("修改采购单"+poc.getPurchaseId()+"的仓库id、入库状态失败！！");
            return message;
        }
        message.setStatus("200");
        message.setFlag(true);
        message.setMsg("修改采购单"+poc.getPurchaseId()+"的仓库id、入库状态成功！！");

        return message;
    }

    /**
     * 根据条件查询品牌信息
     *
     * @param opc
     * @return
     */
    @RequestMapping("/brand")
    public ResultMessage queryBrand(OtherPurchaseCondition opc) {
        List<CBrand> brands = queryService.queryBrand(opc);

        ResultMessage message = new ResultMessage();
        message.setStatus("200");
        message.setFlag(true);
        message.setResultData(brands);
        return message;
    }

    /**
     * 根据品牌id 查询类型
     *
     * @param opc
     * @return
     */
    @RequestMapping("/type")
    public ResultMessage queryType(OtherPurchaseCondition opc) {
        ResultMessage message = new ResultMessage();
        List<CType> types = queryService.queryTypeByBrandId(opc);
        message.setStatus("200");
        message.setFlag(true);
        message.setResultData(types);
        return message;
    }

    /**
     * 根据查询商品信息
     *
     * @param opc
     * @return
     */
    @RequestMapping("/product")
    public ResultMessage queryProduct(OtherPurchaseCondition opc) {
        ResultMessage message = new ResultMessage();
        List<CProduct> products = queryService.queryProductByTypeId(opc);
        message.setStatus("200");
        message.setFlag(true);
        message.setResultData(products);
        return message;
    }

    /**
     * 查询厂商
     *
     * @param opc
     * @return
     */
    @RequestMapping("/firm")
    public ResultMessage queryFirm(OtherPurchaseCondition opc) {
        ResultMessage message = new ResultMessage();
        List firm = queryService.queryFirm(opc);
        message.setStatus("200");
        message.setFlag(true);
        message.setResultData(firm);
        return message;
    }

}
